﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace Common.Helpers
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString ImageLink(this HtmlHelper html, string linkUrl, string imageSrc, object htmlAttributes = null)
        {
            TagBuilder link = new TagBuilder("a");
            link.Attributes.Add("href", linkUrl);
            link.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            TagBuilder image = new TagBuilder("img");
            image.Attributes.Add("src", imageSrc);
            link.InnerHtml += image.ToString(TagRenderMode.Normal);
            return new MvcHtmlString(link.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputTextRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            MvcHtmlString container = html.FormContainer(expression, html.TextBoxFor(expression, htmlAttributes).ToString(), "textBoxField");
            return html.RenderFormRow(expression, container.ToString());
        }

        public static MvcHtmlString FormLabel<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            TagBuilder labelField = new TagBuilder("div");
            labelField.InnerHtml += html.LabelFor(expression, new { @class = "labelField" });
            labelField.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(labelField.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString FormInputDropDown<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression,IEnumerable<SelectListItem>  items,object htmlAttributes = null)
        {
            MvcHtmlString container = html.FormContainer(expression, html.DropDownListFor(expression, items, htmlAttributes).ToString(), "dropDownField");
            return html.RenderFormRow(expression, container.ToString());
        }

        public static MvcHtmlString FormInputDateField<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
        {
            MvcHtmlString container = html.FormContainer(expression, html.TextBoxFor(expression, htmlAttributes).ToString(), "dateBoxField");
            return html.RenderFormRow(expression, container.ToString());
        }

        public static MvcHtmlString FormInputSubmit(this HtmlHelper html, string buttonText, object htmlAttributes = null, string containerClass = null)
        {     
            var submitButton = new TagBuilder("input");
            submitButton.AddCssClass("submit");
            submitButton.Attributes.Add("type", "submit");
            submitButton.Attributes.Add("value", buttonText);
            submitButton.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            MvcHtmlString container = html.FormContainer(submitButton.ToString(TagRenderMode.Normal), "submitButtonRow");
            return container;
        }

        public static MvcHtmlString RenderFormRow<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string formContent)
        {
            string containerContent = string.Concat(html.FormLabel(expression), formContent);
            return html.FormContainer(expression, containerContent, "formRow");
        }

        public static MvcHtmlString FormContainer<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string containerContent,string cssClass ,object htmlAttributes = null)
        {
            return html.FormContainer(containerContent, cssClass, htmlAttributes);
        }

        public static MvcHtmlString FormContainer(this HtmlHelper html, string containerContent, string cssClass, object htmlAttributes = null)
        {
            TagBuilder tagBuilder = new TagBuilder("div");
            tagBuilder.AddCssClass(cssClass);
            tagBuilder.InnerHtml += containerContent;
            tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }
    }
}